<?php

class loan_model extends CI_Model 
{
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
	
	// Get Loan based on Loan ID
	function getLoanByID( $access_id )
	{
		$this->db->where('access_id', $access_id);
		$this->db->from('tbl_loan');
		return $this->db->get();
	}
	
	// Get Access ID(s) based on Subscriber ID
	function getAccessIDsForSubscriber( $subscriber_id )
	{
		// Select only author_id in result set
		$this->db->select('access_id');
		$this->db->where('subscriber_id', $subscriber_id);
		$this->db->from('tbl_loan');
		$result = $this->db->get()->result();
		
		// Convert result to array and return them
		$ret_array = array();
		foreach ($result as $key=>$value)
			array_push($ret_array, $value->access_id);
		return $ret_array;
	}

	// Get Subscriber ID based on Volume (Access ID)
	function getSubscriberIDForVolume( $access_id )
	{
		// Select only subscriber_id in result set
		$this->db->where('access_id', $access_id);
		$this->db->from('tbl_loan');
		return $this->db->get();
	}
	
	// Get Subscriber ID(s) based on Volume (Access ID)
	function getSubscriberIDsForVolume( $access_id )
	{
		// Select only subscriber_id in result set
		$this->db->select('subscriber_id, loan_date, due_date');
		$this->db->where('access_id', $access_id);
		$this->db->from('tbl_loan');
		$result = $this->db->get()->result();
		
		// Convert result to array and return them
		$ret_array = array();
		foreach ($result as $key=>$value)
			array_push($ret_array, $value->subscriber_id);
		return $ret_array;
	}
	
	// Get Volumes loaned by subscriber
	function getVolumesLoanedBySubscriber( $subscriber_id )
	{
		$this->db->select('*');
		$this->db->from('tbl_loan');
		$this->db->join('tbl_volume', 'tbl_volume.access_id = tbl_loan.access_id');
		$this->db->where('tbl_loan.subscriber_id', $subscriber_id);
		return $this->db->get();
	}
	
	// Get last Loan ID
	function getLastAccessID( )
	{
		$this->db->select_max('loan_id');
		return $this->db->get('tbl_loan')->row()->loan_id;
	}

	// Add loan
	function addLoan( $data_array )
	{
		$this->db->insert('tbl_loan', $data_array); 
	}
	
	// Delete loan based on Loan ID
	function deleteLoanByID( $access_id )
	{
		$delete_where_array = array('access_id' => $access_id );
	
		$this->db->delete('tbl_loan', $delete_where_array );
	}
	
	// Updates loans based on Loan ID
	function updateLoan( $access_id, $data_array )
	{
		$update_where_array = array('access_id' => $access_id);
	
		$this->db->update('tbl_loan', $data_array, $update_where_array); 
	}
}

?>